﻿// Decompiled with JetBrains decompiler
// Type: Netick.NativeArray`1
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;
using System.Runtime.CompilerServices;

#nullable disable
namespace Netick;

internal struct NativeArray<T> where T : unmanaged
{
  public unsafe int* Ptr;
  public int BufferSize;

  public unsafe bool IsValid => (IntPtr) this.Ptr != IntPtr.Zero;

  public unsafe T* GetPtr() => (T*) this.Ptr;

  public int Length
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)] get => this.BufferSize;
  }

  public int GetByteSize() => sizeof (T) * this.BufferSize;

  public unsafe T this[int i]
  {
    [MethodImpl(MethodImplOptions.AggressiveInlining)] get
    {
      if (i < 0 || i > this.BufferSize - 1)
        throw new IndexOutOfRangeException();
      return ((T*) this.Ptr)[i];
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)] set
    {
      if (i < 0 || i > this.BufferSize - 1)
        throw new IndexOutOfRangeException();
      ((T*) this.Ptr)[i] = value;
    }
  }

  [MethodImpl(MethodImplOptions.AggressiveInlining)]
  public unsafe void UnsafeSet(int index, T element) => ((T*) this.Ptr)[index] = element;

  public unsafe void ExpandCapacity(IAllocator allocator)
  {
    NativeUtils.ResizeBuffer(allocator, this.BufferSize * 2, sizeof (T), ref this.BufferSize, ref this.Ptr);
  }

  public unsafe void Allocate(IAllocator allocator, int initialCapacity)
  {
    NativeUtils.Allocate(allocator, true, initialCapacity, sizeof (T), ref this.BufferSize, ref this.Ptr);
  }

  public unsafe void Deallocate(IAllocator allocator)
  {
    NativeUtils.Deallocate(allocator, ref this.Ptr);
  }
}
